import Vue from 'vue'
import Vuex from 'vuex'
/* api */
import * as RoleApi from '@src/api/RoleApi'
/* enum */
import { RootVuexPropertyEnum } from '@src/modules/account/roleService/model/enum/VuexPropertyEnum'
/* type */
import { RoleViewInitDataType, sampleInitData } from '@src/modules/account/roleService/types'
/* model */
import { RootStoreStateType } from '@src/modules/account/roleService/store/model'
import { RoleViewModeEnum } from '@src/modules/account/roleService/model'
import { 
  RoleAddUserListModel, 
  RoleAuthModel, 
  RoleAuthSaveModel, 
  RoleAutoAuthSaveModel, 
  RoleBatchDeleteRoleModel, 
  RoleBatchDeleteUserModel, 
  RoleCheckNameModel, 
  RoleDisPlayOrHidePhoneModel, 
  RoleQueryShieldPhoneModel, 
  RoleResetModel, 
  RoleTreeGetModel, 
  RoleUserListModel 
} from '@model/param/in/Role'
import { RoleAddUserListResult, RoleAuthResult, RoleBatchDeleteUserResult, RoleUserListResult } from '@model/param/out/Role'
import LoginUser from '@model/entity/LoginUser/LoginUser'
import Role, { RoleAuthGroup, RoleType } from '@model/entity/Role/Role'
import Page from '@model/Page.ts'
import MsgModel from '@model/MsgModel'
import Result from '@model/Result'
/* util */
import { isNumber, isNotNumber } from '@src/util/type'
/* service */
import RoleService from '@service/RoleService'
import Log from '@src/util/log'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    initData: sampleInitData,
    currentSelectRole: new RoleType(),
    userPage: new Page(),
    loading: false,
    tableSelection: [],
    authGroups: [],
    roles: [],
    mode: RoleViewModeEnum.UserView,
    authGroupsByCreate: [],
    unAssignedUserNum: 0,
    isExportEncryption: false,
    currentProduct: ''
  },
  getters: {
    roles: (state: RootStoreStateType) => {
      return state.initData.rolesJson || []
    },
    systemRoles(state: RootStoreStateType) {
      return state.roles.filter(role => RoleService.isSystemRole(role))
    },
    customRoles(state: RootStoreStateType) {
      return state.roles.filter(role => RoleService.isCustomRole(role))
    },
    auths(state: RootStoreStateType) {
      return state.initData.authorities || {}
    },
    userList(state: RootStoreStateType) {
      return state.userPage?.list || []
    },
    tenantType(state: RootStoreStateType) {
      return state.initData.tenantType || null
    },
    isUserViewMode(state: RootStoreStateType) {
      return state.mode === RoleViewModeEnum.UserView
    },
    isServiceViewwMode(state: RootStoreStateType) {
      return state.mode === RoleViewModeEnum.ServiceView
    },
    isEditMode(state: RootStoreStateType) {
      return state.mode === RoleViewModeEnum.Edit
    },
    isCreateMode(state: RootStoreStateType) {
      return state.mode === RoleViewModeEnum.Create
    },
    isAssignedUserViewMode(state: RootStoreStateType) {
      return state.mode === RoleViewModeEnum.AssignedUserView
    }
  },
  mutations: {
    [RootVuexPropertyEnum.SetInitData](state: RootStoreStateType, value: RoleViewInitDataType) {
      state.initData = value
    },
    [RootVuexPropertyEnum.SetCurrentSelectRole](state: RootStoreStateType, value: RoleType) {
      state.currentSelectRole = value
    },
    [RootVuexPropertyEnum.SetUserPage](state: RootStoreStateType, value: Page<LoginUser[]>) {
      state.userPage = value
    },
    [RootVuexPropertyEnum.SetLoading](state: RootStoreStateType, value: boolean) {
      state.loading = value
    },
    [RootVuexPropertyEnum.SetTableSelection](state: RootStoreStateType, value: LoginUser[]) {
      state.tableSelection = value
    },
    [RootVuexPropertyEnum.SetAuthGroups](state: RootStoreStateType, value: RoleAuthGroup[]) {
      state.authGroups = value
    },
    [RootVuexPropertyEnum.SetAuthGroupsByCreate](state: RootStoreStateType, value: RoleAuthGroup[]) {
      state.authGroupsByCreate = value
    },
    [RootVuexPropertyEnum.SetRoles](state: RootStoreStateType, value: RoleType[]) {
      state.roles = value
    },
    [RootVuexPropertyEnum.SetMode](state: RootStoreStateType, value: RoleViewModeEnum) {
      state.mode = value
    },
    [RootVuexPropertyEnum.SetUnAssignedUserNum](state: RootStoreStateType, value: number) {
      state.unAssignedUserNum = value
    },
    [RootVuexPropertyEnum.SetIsExportEncryption](state: RootStoreStateType, value: boolean) {
      state.isExportEncryption = value
    },
    [RootVuexPropertyEnum.SetCurrentProduct](state: RootStoreStateType, value: string) {
      state.currentProduct = value
    },
  },
  actions: {
    [RootVuexPropertyEnum.FetchUserList]({ commit, state }, params: RoleUserListModel): RoleUserListResult {
      return RoleApi.userList(params)
    },
    [RootVuexPropertyEnum.ProviderList]({ commit, state }, params: RoleUserListModel): RoleUserListResult {
      return RoleApi.GetProviderList(params)
    },
    [RootVuexPropertyEnum.FetchAddUserList]({ commit, state }, params: RoleAddUserListModel): RoleAddUserListResult {
      return RoleApi.roleAddUser(params)
    },
    [RootVuexPropertyEnum.FetchRoleDeleteUser]({ commit, state }, params: RoleBatchDeleteUserModel): RoleBatchDeleteUserResult {
      return RoleApi.removeProvider(params)
    },
    [RootVuexPropertyEnum.FetchRoleDelete]({ commit, state }, params: RoleBatchDeleteRoleModel): void {
      return RoleApi.roleDelete(params)
    },
    [RootVuexPropertyEnum.FetchRoleAllotRoles]({ commit, state }, params: RoleBatchDeleteRoleModel): void {
      return RoleApi.roleAssignRoles(params)
    },
    [RootVuexPropertyEnum.FetchAuthGroups]({ commit, state }, params: RoleAuthModel): void {
      return (
        RoleApi.getRoleServiceAuth(params).then((result: Result<RoleAuthResult>) => {
          return result
        })
      )
    },
    [RootVuexPropertyEnum.FetchRoleAuthUpdate]({ commit, state }, params: RoleAuthSaveModel): void {
      return RoleApi.roleAuthUpdate(params)
    },
    [RootVuexPropertyEnum.FetchRoleTree]({ commit, state }, params: RoleTreeGetModel): void {
      // return RoleApi.getRoleTree(params)
      // @ts-ignore
      return RoleApi.getRoleServiceTree(params)
    },
    [RootVuexPropertyEnum.FetchRoleReset]({ commit, state }, params: RoleResetModel): void {
      return RoleApi.roleDelete(params)
    },
    [RootVuexPropertyEnum.FetchRoleCheckName]({ commit, state }, params: RoleCheckNameModel): void {
      return RoleApi.roleCheckRoleName(params)
    },
    [RootVuexPropertyEnum.FetchRoleAutoAuthSave]({ commit, state }, params: RoleAutoAuthSaveModel): void {
      return RoleApi.roleAutoAuthSave(params)
    },
    [RootVuexPropertyEnum.FetchRoleQueryShieldPhone]({ commit, state }, params: RoleQueryShieldPhoneModel): void {
      return RoleApi.roleQueryShieldPhone(params)
    },
    [RootVuexPropertyEnum.FetchRoleDisPlayOrHidePhone]({ commit, state }, params: RoleDisPlayOrHidePhoneModel): void {
      return RoleApi.roleDisPlayOrHidePhone(params)
    },
  }
})

export default store
